package com.ouc.util.impl;

import com.ouc.entity.*;
import com.ouc.util.Encoding;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;

/**
 * @Name: EncodingImpl
 * @Description:
 * @Author: Mr.Tong
 */
@Component
public class EncodingImpl implements Encoding {

    @Override
    public String lineEncoding(Line line) {
        StringBuilder stringBuilder = new StringBuilder();
        //拼接线的类型
        stringBuilder.append(line.getMaterialType());
        //格式化线的口径
        DecimalFormat df = new DecimalFormat("00");
        String numStr = df.format(line.getCaliber());
        //拼接线的口径
        StringBuilder resultStr = stringBuilder.append(numStr);
        return resultStr.toString();
    }

    @Override
    public String shapeEncoding(Shape shape) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(shape.getShapeType());
        DecimalFormat df = new DecimalFormat("00");
        String numStr = null;
        if (shape instanceof Circle) {
            Circle circle = (Circle) shape;
            numStr = df.format(circle.getRadius());
        } else if (shape instanceof Rectangle) {
            Rectangle rectangle = (Rectangle) shape;
            numStr = df.format(rectangle.getHeight()) + df.format(rectangle.getWidth());
        } else if (shape instanceof Triangle) {
            Triangle triangle = (Triangle) shape;
            numStr = df.format(triangle.getEdgeOne()) + df.format(triangle.getEdgeTwo()) + df.format(triangle.getEdgeThree());
        }
        StringBuilder resultStr = stringBuilder.append(numStr);
        return resultStr.toString();
    }
}
